package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/10  20:39
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");

        //获取session
        HttpSession session = req.getSession();
        //本身为空 直接跳转
        if(session == null){
            resp.sendRedirect("blog_login.html");
            return;
        }
        //session不为空 销毁session并解绑任何数据
        session.invalidate();
        //销毁之后跳转到登录页
        resp.sendRedirect("blog_login.html");
    }
}
